home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / codecs / qdmediamaker / qdmmaker.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  9.3 KB  |  398 lines

  1. //////////
  2. //
  3. //    File:        QDMMaker.c
  4. //
  5. //    Contains:    Code to create movies that use the QuickDraw derived media handler.
  6. //
  7. //    Written by:    Tim Monroe
  8. //                Based on MyMakeMediaMovies code written by John Wang.
  9. //
  10. //    Copyright:    © 1993-1999 by Apple Computer, Inc., all rights reserved.
  11. //
  12. //    Change History (most recent first):
  13. //
  14. //       <4>         02/03/99    rtm        reworked prompt and filename handling to remove "\p" sequences
  15. //       <3>         01/28/99    rtm        added EndianU32_NtoB to kQDMH_Version; runs fine on Windows
  16. //       <2>         01/14/99    rtm        conversion to personal coding style; runs fine on Mac
  17. //       <1>         02/25/93    jw        first file
  18. //
  19. //    This code builds four sample movies that use the QuickDraw derived media handler. For 
  20. //    complete information, see John Wang's article on derived media handlers in develop,
  21. //    issue 14 (June 1993).
  22. //       
  23. //////////
  24.  
  25. //////////
  26. //
  27. // header files
  28. //
  29. //////////
  30.  
  31. #include "QDMMaker.h"
  32.  
  33.  
  34. //////////
  35. //
  36. // global variables
  37. //
  38. //////////
  39.  
  40. WindowPtr                        gWindow = NULL;
  41.  
  42.  
  43. //////////
  44. //
  45. // QDMM_AddRowsSamples
  46. // Add samples for the rows movie to the specified media.
  47. //
  48. //////////
  49.  
  50. static OSErr QDMM_AddRowsSamples (Media theMedia, short theWidth, short theHeight, QDrawDescriptionHandle theQDDesc)
  51. {
  52.     long                        myIndex;
  53.     Rect                        myDrawRect;
  54.     Rect                        myWindRect;
  55.     RGBColor                    myColor;
  56.     PicHandle                    myPict = NULL;
  57.     OSErr                        myErr = noErr;
  58.  
  59.     // set our drawing rectangles
  60.     MacSetRect(&myDrawRect, 0, 0, theWidth, theHeight / 5);
  61.     MacSetRect(&myWindRect, 0, 0, theWidth, theHeight);
  62.     
  63.     for (myIndex = 0; myIndex < 10; myIndex++) {
  64.         short                    mySync;
  65.         
  66.         myPict = OpenPicture(&myWindRect);
  67.         
  68.         if (myIndex == 0) {
  69.             EraseRect(&myWindRect);
  70.             mySync = 0;
  71.         } else {
  72.             mySync = mediaSampleNotSync;
  73.         }
  74.         
  75.         myColor.red = myColor.green = myColor.blue = (myIndex * 25) << 8;
  76.         RGBForeColor(&myColor);
  77.         
  78.         PaintRect(&myDrawRect);
  79.         ClosePicture();
  80.  
  81.         myErr = AddMediaSample(theMedia, (Handle)myPict, 0, GetHandleSize((Handle)myPict), 600, (SampleDescriptionHandle)theQDDesc, 1, mySync, NULL);
  82.             
  83.         DrawPicture(myPict, &myWindRect);
  84.         KillPicture(myPict);
  85.         MacOffsetRect(&myDrawRect, 0, theHeight / 10);    
  86.     }
  87.     
  88.     return(myErr);
  89. }
  90.  
  91.  
  92. //////////
  93. //
  94. // QDMM_AddLinesSamples
  95. // Add samples for the lines movie to the specified media.
  96. //
  97. //////////
  98.  
  99. static OSErr QDMM_AddLinesSamples (Media theMedia, short theWidth, short theHeight, QDrawDescriptionHandle theQDDesc)
  100. {
  101.     short                        myIndex;
  102.     Rect                        myDrawRect;
  103.     Rect                        myWindRect;
  104.     RGBColor                    myColor;
  105.     PicHandle                    myPict = NULL;
  106.     OSErr                        myErr = noErr;
  107.  
  108.     // set our drawing rectangles
  109.     MacSetRect(&myDrawRect, 0, 0, theWidth, theHeight);
  110.     MacSetRect(&myWindRect, 0, 0, theWidth, theHeight);
  111.     
  112.     for (myIndex = 0; myIndex < theWidth; myIndex++) {
  113.         short                    mySync;
  114.  
  115.         myPict = OpenPicture(&myWindRect);
  116.         myColor.red = (myIndex & 0xff) << 8;
  117.         myColor.green = ((255 - myIndex) & 0xff) << 8;
  118.         myColor.blue = ((128 - myIndex) & 0xff) << 8;
  119.         
  120.         if (myIndex == 0) {
  121.             EraseRect(&myWindRect);
  122.             mySync = 0;
  123.         } else {
  124.             mySync = mediaSampleNotSync;
  125.         }
  126.         
  127.         RGBForeColor(&myColor);
  128.         MoveTo(myIndex, 0);
  129.         MacLineTo(myIndex, theHeight);
  130.         ClosePicture();
  131.         DrawPicture(myPict, &myWindRect);
  132.         
  133.         myErr = AddMediaSample(theMedia, (Handle)myPict, 0, GetHandleSize((Handle)myPict), 20, (SampleDescriptionHandle)theQDDesc, 1, mySync, NULL);
  134.         
  135.         KillPicture(myPict);
  136.     }
  137.     
  138.     return(myErr);
  139. }
  140.  
  141.  
  142. //////////
  143. //
  144. // QDMM_AddBoxesSamples
  145. // Add samples for the boxes movie to the specified media.
  146. //
  147. //////////
  148.  
  149. static OSErr QDMM_AddBoxesSamples (Media theMedia, short theWidth, short theHeight, QDrawDescriptionHandle theQDDesc)
  150. {
  151.     long                        myIndex;
  152.     Rect                        myDrawRect;
  153.     Rect                        myWindRect;
  154.     RGBColor                    myColor;
  155.     PicHandle                    myPict = NULL;
  156.     OSErr                        myErr = noErr;
  157.  
  158.     // set our drawing rectangles
  159.     MacSetRect(&myDrawRect, 0, 0, theWidth, theHeight);
  160.     MacSetRect(&myWindRect, 0, 0, theWidth, theHeight);
  161.     
  162.     for (myIndex = 0; myIndex < 50; myIndex++) {
  163.         short                    mySync;
  164.  
  165.         myPict = OpenPicture(&myWindRect);
  166.         myColor.red = ((myIndex * 10) & 0xff) << 8;
  167.         myColor.green = ((myIndex * 20) & 0xff) << 8;
  168.         myColor.blue = ((myIndex * 30) & 0xff) << 8;
  169.         RGBForeColor(&myColor);
  170.         
  171.         if (myIndex == 0) {
  172.             EraseRect(&myWindRect);
  173.             mySync = 0;
  174.         } else {
  175.             mySync = mediaSampleNotSync;
  176.         }
  177.         
  178.         PaintRect(&myDrawRect);
  179.         ClosePicture();
  180.         DrawPicture(myPict, &myWindRect);
  181.         MacInsetRect(&myDrawRect, theWidth / 100, theHeight / 100);    
  182.         
  183.         myErr = AddMediaSample(theMedia, (Handle)myPict, 0, GetHandleSize((Handle)myPict), 40, (SampleDescriptionHandle)theQDDesc, 1, mySync, NULL);
  184.         
  185.         KillPicture(myPict);
  186.     }
  187.     
  188.     return(myErr);
  189. }
  190.  
  191.  
  192. //////////
  193. //
  194. // QDMM_AddBallSamples
  195. // Add samples for the ball movie to the specified media.
  196. //
  197. //////////
  198.  
  199. static OSErr QDMM_AddBallSamples (Media theMedia, short theWidth, short theHeight, QDrawDescriptionHandle theQDDesc)
  200. {
  201.     long                        myIndex;
  202.     Rect                        myDrawRect;
  203.     Rect                        myWindRect;
  204.     RGBColor                    myColor;
  205.     PicHandle                    myPict = NULL;
  206.     short                        lx, ly, x, y, vx, vy;
  207.     OSErr                        myErr = noErr;
  208.  
  209.     // set our drawing rectangles
  210.     MacSetRect(&myDrawRect, 0, 0, 30, 30);
  211.     MacSetRect(&myWindRect, 0, 0, theWidth, theHeight);
  212.  
  213.     EraseRect(&myWindRect);
  214.     
  215.     lx = ly = x = y = 10;
  216.     vx = 5, vy = 3;
  217.     for (myIndex = 0; myIndex < 1000; myIndex++) {
  218.         short                    mySync;
  219.  
  220.         myPict = OpenPicture(&myWindRect);
  221.         if ((myIndex % 10) == 0) {
  222.             mySync = 0;
  223.             myColor.red = ((myIndex * 15) & 0xff) << 8;
  224.             myColor.green = ((myIndex * 10) & 0xff) << 8;
  225.             myColor.blue = ((myIndex * 5) & 0xff) << 8;
  226.             RGBBackColor(&myColor);
  227.             EraseRect(&myWindRect);
  228.         } else {
  229.             mySync = mediaSampleNotSync;
  230.         }
  231.         
  232.         myColor.red = ((myIndex * 5) & 0xff) << 8;
  233.         myColor.green = ((myIndex * 10) & 0xff) << 8;
  234.         myColor.blue = ((myIndex * 15) & 0xff) << 8;
  235.         RGBForeColor(&myColor);
  236.         MacSetRect(&myDrawRect, lx - 15, ly - 15, lx + 15, ly + 15);
  237.         EraseOval(&myDrawRect);
  238.         MacSetRect(&myDrawRect, x - 15, y - 15, x + 15, y + 15);
  239.         PaintOval(&myDrawRect);
  240.         lx = x;
  241.         ly = y;
  242.         x += vx;
  243.         y += vy;
  244.         if (x > theWidth) {
  245.             x = theWidth;
  246.             vx = -vx;
  247.         } else if (x < 0) {
  248.             x = 0;
  249.             vx = -vx;
  250.         }
  251.         
  252.         if (y > theHeight) {
  253.             y = theHeight;
  254.             vy = -vy;
  255.         } else if (y < 0) {
  256.             y = 0;
  257.             vy = -vy;
  258.         }
  259.         
  260.         ClosePicture();
  261.         DrawPicture(myPict, &myWindRect);
  262.         
  263.         myErr = AddMediaSample(theMedia, (Handle)myPict, 0, GetHandleSize((Handle)myPict), 40, (SampleDescriptionHandle)theQDDesc, 1, mySync, NULL);
  264.  
  265.         KillPicture(myPict);
  266.     }
  267.     
  268.     return(myErr);
  269. }
  270.  
  271.  
  272. //////////
  273. //
  274. // QDMM_MakeQDMovie
  275. // Make a movie with a QuickDraw media track.
  276. //
  277. //////////
  278.  
  279. void QDMM_MakeQDMovie (UInt16 theMenuItem, short theWidth, short theHeight)
  280. {
  281.     short                    myResRefNum = 0;
  282.     short                    myResID = movieInDataForkResID;
  283.     Movie                    myMovie = NULL;
  284.     Track                    myTrack;
  285.     Media                    myMedia;
  286.     StandardFileReply        myReply;
  287.     long                    myFlags = createMovieFileDeleteCurFile | createMovieFileDontCreateResFile;
  288.     Rect                    myWindRect;
  289.     QDrawDescriptionHandle    myQDDesc = NULL;
  290.     StringPtr                 myMoviePrompt = QTUtils_ConvertCToPascalString(kSaveQDMoviePrompt);
  291.     StringPtr                 myMovieFileName = QTUtils_ConvertCToPascalString(kSaveQDMovieFileName);
  292.     OSErr                    myErr = noErr;
  293.     
  294.     //////////
  295.     //
  296.     //    set window size and rectangle
  297.     //
  298.     //////////
  299.  
  300.     SizeWindow(gWindow, theWidth, theHeight, false);
  301.     MacSetRect(&myWindRect, 0, 0, theWidth, theHeight);
  302.     EraseRect(&myWindRect);
  303.     
  304.     //////////
  305.     //
  306.     // create a new movie file
  307.     //
  308.     //////////
  309.  
  310.     // ask the user for the name of the new movie file
  311.     StandardPutFile(myMoviePrompt, myMovieFileName, &myReply);
  312.     if (!myReply.sfGood)
  313.         goto bail;
  314.  
  315.     // create a movie file for the destination movie
  316.     myErr = CreateMovieFile(&myReply.sfFile, FOUR_CHAR_CODE('TVOD'), smSystemScript, myFlags, &myResRefNum, &myMovie);
  317.     if (myErr != noErr)
  318.         goto bail;
  319.  
  320.     //////////
  321.     //
  322.     // create the QuickDraw track and media
  323.     //
  324.     //////////
  325.     
  326.     myTrack = NewMovieTrack(myMovie, ((long)theWidth << 16), ((long)theHeight << 16), kNoVolume);
  327.     myMedia = NewTrackMedia(myTrack, kQDMH_MediaType, kQDMediaTimeScale, NULL, (OSType)0);
  328.         
  329.     //////////
  330.     //
  331.     // add samples to the QuickDraw track's media
  332.     //
  333.     //////////
  334.     
  335.     BeginMediaEdits(myMedia);
  336.         
  337.     // create a sample description
  338.     myQDDesc = (QDrawDescriptionHandle)NewHandleClear(sizeof(QDrawDescription));
  339.     if (myQDDesc == NULL)
  340.         goto bail;
  341.     (**myQDDesc).size = sizeof(QDrawDescription);
  342.     (**myQDDesc).type = kQDMH_MediaType;
  343. #if HANDLER_SWAPS_SAMPLE_DESC
  344.     (**myQDDesc).version = kQDMH_Version;
  345. #else
  346.     (**myQDDesc).version = EndianU32_NtoB(kQDMH_Version);
  347. #endif
  348.  
  349.     // add media samples
  350.     switch (theMenuItem) {
  351.         case IDM_ROWS:
  352.             myErr = QDMM_AddRowsSamples(myMedia, theWidth, theHeight, myQDDesc);
  353.             break;
  354.         case IDM_LINES:
  355.             myErr = QDMM_AddLinesSamples(myMedia, theWidth, theHeight, myQDDesc);
  356.             break;
  357.         case IDM_BOXES:
  358.             myErr = QDMM_AddBoxesSamples(myMedia, theWidth, theHeight, myQDDesc);
  359.             break;
  360.         case IDM_BALL:
  361.             myErr = QDMM_AddBallSamples(myMedia, theWidth, theHeight, myQDDesc);
  362.             break;
  363.         default:
  364.             break;
  365.     }    // switch (theMenuItem)
  366.  
  367.     EndMediaEdits(myMedia);
  368.     
  369.     if (myErr != noErr)
  370.         goto bail;
  371.     
  372.     // add the media to the track
  373.     InsertMediaIntoTrack(myTrack, 0, 0, GetMediaDuration(myMedia), fixed1);
  374.     
  375.     //////////
  376.     //
  377.     // finish up
  378.     //
  379.     //////////
  380.     
  381.     // add the movie resource to the movie file
  382.     AddMovieResource(myMovie, myResRefNum, &myResID, myReply.sfFile.name);
  383.     
  384. bail:
  385.     free(myMoviePrompt);
  386.     free(myMovieFileName);
  387.     
  388.     if (myResRefNum != 0)
  389.         CloseMovieFile(myResRefNum);
  390.  
  391.     if (myMovie != NULL)
  392.         DisposeMovie(myMovie);
  393.         
  394.     if (myQDDesc != NULL)
  395.         DisposeHandle((Handle)myQDDesc);
  396. }
  397.  
  398.